package codepulse.blogmoon.cn.code.filter;

import codepulse.blogmoon.cn.common.context.Context;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class WebFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String teamId = request.getHeader("teamId");
        if (StringUtils.isNoneBlank(teamId)) {
            Context.get().setTeamId(Long.parseLong(teamId));
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
